package com.example.bbs.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ArrayRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bbs.R;
import com.example.bbs.util.DisplayUtil;

import java.util.Objects;

public class BottomListDialog extends AlertDialog {

    static class AlertParams {
        public boolean mCancelable;
        public CharSequence[] mItems;
        public OnClickListener mOnClickListener;
    }

    public static class Builder extends AlertDialog.Builder {

        AlertParams P = new AlertParams();
        BottomListDialog dialog;

        public Builder(@NonNull Context context) {
            super(context);
        }

        @Override
        public Builder setCancelable(boolean cancelable) {
            P.mCancelable = cancelable;
            return this;
        }

        @Override
        public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
            P.mItems = getContext().getResources().getTextArray(itemsId);
            P.mOnClickListener = listener;
            return this;
        }

        public Builder setItems(CharSequence[] items, final OnClickListener listener) {
            P.mItems = items;
            P.mOnClickListener = listener;
            return this;
        }

        @NonNull
        @Override
        public BottomListDialog create() {
            dialog = new BottomListDialog(getContext(), P.mItems, P.mOnClickListener);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            return dialog;
        }

        @Override
        public BottomListDialog show(){
            create().show();

            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.BOTTOM);
            window.setContentView(R.layout.dialog_bottom_list);
            // TODO(Low): 这里动画时间不可控，查一下原因。
            window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
            window.setLayout(DisplayUtil.screenSize.x, LinearLayout.LayoutParams.WRAP_CONTENT);

            dialog.initRecyclerView();
            return dialog;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public int pos;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(v -> mOnClickListener.onClick(BottomListDialog.this, pos));
        }

        public void setText(CharSequence charSequence) {
            TextView tvBottomListItem = itemView.findViewById(R.id.tv_bottom_list_item);
            tvBottomListItem.setText(charSequence);
        }
    }

    public class Adapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.view_item_bottom_list_dialog, null);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            holder.pos = position; // 注意，这里默认 position 不会改变
            holder.setText(mItems[position]);
        }

        @Override
        public int getItemCount() {
            return mItems.length;
        }
    }

    CharSequence[] mItems;
    OnClickListener mOnClickListener;

    public BottomListDialog(@NonNull Context context, CharSequence[] items, OnClickListener onClickListener) {
        super(context);
        mItems = items;
        mOnClickListener = onClickListener;
    }

    public void initRecyclerView() {
        RecyclerView rv = findViewById(R.id.rv_bottom_list);
        assert rv != null;
        rv.setAdapter(new Adapter());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(layoutManager);
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getContext(), R.drawable.divider)));
        rv.addItemDecoration(divider);
    }
}
